package com.fz.service.system.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fz.common.ResponseEntity;
import com.fz.dto.system.role.RoleAdd;
import com.fz.dto.system.role.RoleMenuDto;
import com.fz.dto.system.role.RoleQuery;
import com.fz.dto.system.role.RoleUpd;
import com.fz.entity.system.Role;
import com.fz.entity.system.RoleMenu;
import com.fz.mapper.system.RoleMapper;
import com.fz.service.system.MenuService;
import com.fz.service.system.RoleService;
import com.fz.utils.StringUtils;
import com.fz.vo.system.menu.MenuTreeVo;
import com.fz.vo.system.menu.RoleMenuTreeVo;
import com.fz.vo.system.menu.MenuVo;
import com.fz.vo.system.role.RoleVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.*;

@Service
@SuppressWarnings("unchecked")
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private MenuService menuService;

    /**
     * @方法描述: 角色管理查询角色，以及菜单
     * @参数:
     * @返回值:
     * @创建人:
     * @创建时间:: 2021/3/22
     */
    @Override
    public ResponseEntity<RoleMenuTreeVo> findRoleList(int pageNum, int pageSize,RoleQuery roleQuery) throws Exception{
        MenuTreeVo menuTreeDtoList = menuService.findMenuTree();
        Page<RoleVo> page = new Page<>(pageNum,pageSize);
        IPage<RoleVo> roleVos = roleMapper.findRoleList(page);
        RoleMenuTreeVo roleMenuTreeVo = new RoleMenuTreeVo();
        roleMenuTreeVo.setMenuTreeVo(menuTreeDtoList);
        roleMenuTreeVo.setRoleVos(roleVos);
        return ResponseEntity.buildSuccess(roleMenuTreeVo);
    }

    /**
     * @方法描述: 前端给用户角色id，获取到之后用来查询拥有的目录
     * @参数:
     * @返回值:
     * @创建人:
     * @创建时间:: 2021/3/22
     */
    @Override
    public ResponseEntity<List<MenuVo>> findMenuById(int roleId) throws Exception{
        List<MenuVo> roleOfMenu =  roleMapper.findMenuByRoleId(roleId);
        return ResponseEntity.buildSuccess(roleOfMenu);
    }

    /**
     * @方法描述: 模糊查询拥有的目录
     * @参数:
     * @返回值:
     * @创建人:
     * @创建时间:: 2021/3/22
     */
    @Override
    public ResponseEntity<List<RoleVo>> findRoleSelect(RoleQuery roleQuery) throws Exception{
        List<RoleVo> roleVos = roleMapper.findRoleVo(roleQuery);
        return ResponseEntity.buildSuccess(roleVos);
    }

    /**
     * @方法描述: 新增角色
     * @参数:
     * @返回值:
     * @创建人:
     * @创建时间:: 2021/3/22
     */
    @Override
    public ResponseEntity addRole(RoleAdd roleAdd) throws Exception{
        Role role = new Role();
        role.setName(roleAdd.getName());
        role.setDataScope(roleAdd.getDataScope());
        role.setLevel(roleAdd.getLevel());
        role.setPermission(roleAdd.getPermission());
        role.setRemark(roleAdd.getRemark());
        roleMapper.insert(role);
        return ResponseEntity.buildSuccess(null);
    }

    /**
     * @方法描述: 根据修改的角色属性，修改数据库
     * @参数:
     * @返回值:
     * @创建人:
     * @创建时间:: 2021/3/22
     */
    @Override
    public ResponseEntity updateRole(RoleUpd roleUpd) throws Exception{
        Role role = new Role();
        role.setId(roleUpd.getId());
        role.setName(roleUpd.getName());
        role.setPermission(roleUpd.getPermission());
        role.setDataScope(roleUpd.getDataScope());
        role.setLevel(roleUpd.getLevel());
        role.setRemark(roleUpd.getRemark());
        roleMapper.updateById(role);
        return ResponseEntity.buildSuccess(role);
    }

    /**
     * @方法描述: 建立权限，角色的全新对应关系
     * @参数:
     * @返回值:
     * @创建人:
     * @创建时间:: 2021/3/22
     */
    @Override
    @Transactional
    public ResponseEntity saveRoleMenu(RoleMenuDto roleMenuDto) throws Exception{
        RoleMenu roleMenu = new RoleMenu();
        roleMenu.setRoleId(roleMenuDto.getRoleid());
        roleMapper.delRoleMenu(roleMenu);
        roleMapper.insertRoleMenu(StringUtils.strToList(roleMenuDto.getMenuids()), roleMenuDto.getRoleid());
        return ResponseEntity.buildSuccess();
    }



}
